/**
 * 导入导出
 */
//下载模板
function downLoadTemplate() {
    var Options = $table.bootstrapTable('getOptions');
    if(Options.columns){
        var columns = Options.columns[0];
        var field = [];
        var title = [];
        var data = {};
        for (var i=0; i<columns.length-1;i++){
            field[i] = columns[i].field;
            title[i] = columns[i].title;
            data[columns[i].field] = columns[i].title
        }
        //var data=JSON.stringify(data);//有表传递参数
    }else{
        var data={};
    }
    var url = downpath+"?tablename="+tablename+"&data="+JSON.stringify(data);
    window.open(url);
}
// 自定义按钮导出数据
function exportData(){
    layer.open({
        type: 1
        ,title: '导出'
        ,area: ['40%']
        ,btn: ['导出', '取消']
        ,content: node()
        ,success: function (layero,index) {
            layui.form.render();
            layui.form.on('select(conditionexport)', function(data){
                var val=data.value;
                if(val){
                    $(".tiao").hide();
                }else{
                    $(".tiao").show();
                }
            });
        },yes: function(index, layero){
            pagnum = $table.bootstrapTable('getOptions').pageSize;//原始配置
            var totalRows = $table.bootstrapTable('getOptions').totalRows;
            var conditionexport = $("#conditionexport").val()
            var number = $("#pagenum").val()
            var condition = $('input[name="condition"]:checked ').val()
            var exportfilename = $("#exportfilename").val()
            var opt = {
                url: nowurl,
                silent: true,
            };
            var Param =getFormJson('searchform');//检索条件
            var exportParam = {};//分页参数
            if(exportfilename==''){
                exportfilename = '数据表';//默认名称
            }
            if(conditionexport){
                opt['pageNumber'] = 1;//起始页
                if(conditionexport=='all'){
                    opt['pageSize']   = totalRows;
                }else{
                    opt['pageSize']   = pagnum;
                }
            }else{
                if(condition==1){
                    opt['pageNumber'] = 1;//起始页
                    if(number>0){
                        opt['pageSize']   = number;
                    }else{
                        opt['pageSize']   = pagnum;
                    }
                }else {
                    opt['pageNumber'] = 1;//起始页
                    if(number>0){
                        opt['pageSize']   = number;
                    }else{
                        opt['pageSize']   = pagnum;
                    }
                }
            }
            Param['pageNumber'] = opt['pageNumber']
            Param['pageSize']   = opt['pageSize']
            opt['queryParams']=Param;
            $table.bootstrapTable('refreshOptions',opt);
            setTimeout(function () {
                report(exportfilename,Param)
            },1000);
            layer.close(index);
        }
    });
}
function node() {
    var str = '';
    str +='<form class="layui-form" action="">'
    str +='                <div class="layui-form-item">'
    str +='                    <label class="layui-form-label">导出条件</label>'
    str +='                    <div class="layui-input-inline">'
    str +='                        <select name="conditionexport" id="conditionexport" lay-filter="conditionexport">'
    str +='                             <option value="">条件导出</option>'
    str +='                             <option value="basic">当前数据</option>'
    str +='                             <option value="all">全部数据</option>'
    str +='                        </select>'
    str +='                    </div>'
    str +='                </div>'
    str +='                <div class="layui-form-item" style="display: block">'
    str +='                    <label class="layui-form-label">导出文件名</label>'
    str +='                    <div class="layui-input-inline" >'
    str +='                         <input type="text" name="exportfilename" value="" id="exportfilename"   class="layui-input">'
    str +='                    </div>'
    str +='                </div>'
    str +='                <div class="layui-form-item tiao" style="display: block">'
    str +='                    <label class="layui-form-label">导出数量</label>'
    str +='                    <div class="layui-input-inline" >'
    str +='                         <input type="text" name="pagenum" value="" id="pagenum"   class="layui-input">'
    str +='                    </div>'
    str +='                </div>'
    str +='                <div class="layui-form-item tiao" style="display: block">'
    str +='                    <label class="layui-form-label">带搜索条件</label>'
    str +='                    <div class="layui-input-block">'
    str +='                        <input type="radio" name="condition" value="0" title="否" checked="">'
    str +='                        <input type="radio" name="condition" value="1" title="是">'
    str +='                     </div>'
    str +='                 </div>'
    str +='</form>'
    return str
}
function report(exportfilename,Param) {
    $table.tableExport({
        type: 'excel',
        exportDataType: "all",
        ignoreColumn: [-1],//忽略某一列的索引
        fileName: exportfilename,//下载文件名称
        export: 'glyphicon-export icon-share',
        onCellHtmlData: function (cell, row, col, data){
            //处理导出内容,自定义某一行、某一列、某个单元格的内容
            //console.log(data);
            return data;
        },
    });
    Param['pageSize']=pagnum;
    $table.bootstrapTable('refresh', Param);//刷新初回到原来的页面
}
function layUpload() {
    //指定允许上传的文件类型
    layui.upload.render({
        elem: '.fileUpload'
        ,url: importpath
        ,accept: 'file' //普通文件
        ,before:function () {
            var Options = $table.bootstrapTable('getOptions');
            if(Options.columns){
                var columns = Options.columns[0];
                var field = [];
                var title = [];
                var data = {};
                for (var i=0; i<columns.length-1;i++){
                    field[i] = columns[i].field;
                    title[i] = columns[i].title;
                    data[columns[i].field] = columns[i].title
                }
                this.data={'tablename':tablename,field:field,title:title,data:JSON.stringify(data)};//有表传递参数
            }else{
                this.data={};
            }
        }
        ,done: function(res){
            layer.msg(res.msg);
            console.log(res);
        }
    });
}